package binarysearchtree

const testVersion = 1

type SearchTreeData struct {
	left  *SearchTreeData
	data  int
	right *SearchTreeData
}

func Bst(input int) *SearchTreeData {
	st := SearchTreeData{}
	st.data = input
	return &st
}

func (st *SearchTreeData) Insert(input int) {
	if input <= st.data {
		if st.left == nil {
			st.left = &SearchTreeData{}
			st.left.data = input
		} else {
			st.left.Insert(input)
		}
	}

	if input > st.data {
		if st.right == nil {
			st.right = &SearchTreeData{}
			st.right.data = input
		} else {
			st.right.Insert(input)
		}
	}
}

func (st *SearchTreeData) MapString(call func(int) string) (r []string) {
	var search []int
	search = st.Search(search)

	for i := 0; i < len(search); i++ {
		r = append(r, call(search[i]))
	}

	return
}

func (st *SearchTreeData) Search(search []int) []int {
	if st.left != nil {
		search = st.left.Search(search)
	}

	search = append(search, st.data)

	if st.right != nil {
		search = st.right.Search(search)
	}

	return search
}

func (st *SearchTreeData) MapInt(call func(int) int) (r []int) {
	var search []int
	search = st.Search(search)

	for i := 0; i < len(search); i++ {
		r = append(r, call(search[i]))
	}

	return
}
